#include "Scuba.h"
#include "Datalyzer.h"

#include "ArgumentParser.h"
#include "Helper.h"

#include <string>
#include <iostream>
#include <conio.h>

using namespace std;

CArgumentParser* gpxArgumentParser;
string gsTarget = "./test.c";

Scuba xScubaDiver;
Datalyzer xDatalyzer;
MinesweeperOptions gxMinesweeperOptions;

int parseCommandLineMinesweeper(int argc, char**argv)
{
	gpxArgumentParser=new CArgumentParser("vaftblrqnh");

	gpxArgumentParser->m_Helper->fillProgramName("nMinesweeper");
	gpxArgumentParser->m_Helper->fillAuthor("SonOfDelphi");
	gpxArgumentParser->m_Helper->fillSummary("Symbol Information Analyzer");
	gpxArgumentParser->m_Helper->fillUsageLine("[OPTIONS] <SourceFile>\nPREVIEW VERSION (debug demo only)");
	gpxArgumentParser->fillHelpForOption('v',"Variables");
	gpxArgumentParser->fillHelpForOption('a',"Attributes/Fields");
	gpxArgumentParser->fillHelpForOption('f',"Function prototypes");
	gpxArgumentParser->fillHelpForOption('t',"Data Types");
	gpxArgumentParser->fillHelpForOption('b',"Bare (No title row)");
	gpxArgumentParser->fillHelpForOption('l',"List methods only");
	gpxArgumentParser->fillHelpForOption('r',"Report (Not in CSV format)");
	gpxArgumentParser->fillHelpForOption('q',"Quiet mode");
	gpxArgumentParser->fillHelpForOption('n',"No Numbering");
	gpxArgumentParser->fillHelpForOption('h',"Displays this help message");

	gpxArgumentParser->m_Helper->fillYear("2009");
	gpxArgumentParser->m_Helper->fillOwner("The Oraclique");
	gpxArgumentParser->m_Helper->fillTailMessage("Part of Neptuner Code-Management Suite.\nBuild:" __DATE__"/"__TIME__  "\nThis program comes with absolutely no warranty.");

	int nParseEndPos = gpxArgumentParser->parse(argc,argv);
	
	if(!gpxArgumentParser->wasParseSuccessful())
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		cerr<<"Invalid option supplied"<<endl;
		return -1;
	}

	gxMinesweeperOptions.m_bNoHeading = gpxArgumentParser->wasOptionSupplied('b');
	gxMinesweeperOptions.m_bListOnly = gpxArgumentParser->wasOptionSupplied('l');
	gxMinesweeperOptions.m_bReport = gpxArgumentParser->wasOptionSupplied('r');
	gxMinesweeperOptions.m_bModeQuiet = gpxArgumentParser->wasOptionSupplied('q');
	gxMinesweeperOptions.m_bNoNumbering = gpxArgumentParser->wasOptionSupplied('n');

	bool bValue;
	bool bOneSupplied = false;

	bValue= gpxArgumentParser->wasOptionSupplied('v');
	gxMinesweeperOptions.m_bVariables	= bValue;
	bOneSupplied |= bValue;

	bValue= gpxArgumentParser->wasOptionSupplied('a');
	gxMinesweeperOptions.m_bAttributes	= bValue;
	bOneSupplied |= bValue;

	bValue= gpxArgumentParser->wasOptionSupplied('f');
	gxMinesweeperOptions.m_bFunctions	= bValue;
	bOneSupplied |= bValue;
	
	bValue= gpxArgumentParser->wasOptionSupplied('t');
	gxMinesweeperOptions.m_bDataTypes	= bValue;  
	bOneSupplied |= bValue;
	
	if(!bOneSupplied)
	{
		gxMinesweeperOptions.m_bVariables	= true;
		gxMinesweeperOptions.m_bAttributes	= true;
		gxMinesweeperOptions.m_bFunctions	= true;
		gxMinesweeperOptions.m_bDataTypes	= true;  
	}


	return nParseEndPos+1;
}

int main(int argc, char**argv)
{
	int nProgArgIndex = parseCommandLineMinesweeper(argc, argv);
	if(nProgArgIndex<0)
	{
		return 1;
	}

	if(gpxArgumentParser->wasOptionSupplied('h'))
	{
		gpxArgumentParser->m_Helper->displayHelp();
		delete gpxArgumentParser;
		return 0;
	}

	if(nProgArgIndex<argc)
	{
			gsTarget=argv[nProgArgIndex];
	}
	else
	{
		if(!gxMinesweeperOptions.m_bModeQuiet)
			cerr<<"Attempting to proceed with default test file: "<<gsTarget<<endl;
	}

    if(!gxMinesweeperOptions.m_bModeQuiet)
		cerr<<"\n<<"<<gsTarget<<">>"<< " DATA/SYMBOL INFORMATION\n\n";

    xScubaDiver.setObserver(&xDatalyzer);
	xScubaDiver.dive(gsTarget);

    xDatalyzer.print(&gxMinesweeperOptions);

    if(!gxMinesweeperOptions.m_bModeQuiet)
	    cerr<<"\n<<"<<gsTarget<<">>"<< " END OF DATA/SYMBOL INFORMATION\n";

	if(argc==1)
	{
		cerr<<"Press any key to continue..";
		_getch();
	}

    return 0;
}


